package com.zb.designmode.decorate.example113;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * <p>文件描述：</p>
 * <p>版权所有： 版权所有(C)2016-2099</p>
 * <p>内容摘要： </p>
 * <p>其他说明： </p>
 * Date 2019-05-06</p>
 * Author Bing.Zhao1
 *
 * @version 1.0
 */
public class MyBufferedReader extends BufferedReader {
    private Reader in;

    private char cb[];
    private int nChars, nextChar;

    private static final int INVALIDATED = -2;
    private static final int UNMARKED = -1;
    private int markedChar = UNMARKED;
    private int readAheadLimit = 0; /* Valid only when markedChar > 0 */

    /**
     * If the next character is a line feed, skip it
     */
    private boolean skipLF = false;

    /**
     * The skipLF flag when the mark was set
     */
    private boolean markedSkipLF = false;

    private static int defaultCharBufferSize = 8192;
    private static int defaultExpectedLineLength = 80;

    public MyBufferedReader(Reader in, int sz) {
        super(in, sz);
        if (sz < 0) {
            throw new IllegalArgumentException("Buffer size <= 0");
        }
        this.in = in;
        cb = new char[sz];
        nextChar = nChars = 0;
    }

    private void fill() throws IOException {
        int dst;
        if (markedChar <= UNMARKED) {
            /* No mark */
            dst = 0;
        } else {
            /* Marked */
            int delta = nextChar - markedChar;
            if (delta >= readAheadLimit) {
                /* Gone past read-ahead limit: Invalidate mark */
                markedChar = INVALIDATED;
                readAheadLimit = 0;
                dst = 0;
            } else {
                if (readAheadLimit <= cb.length) {
                    /* Shuffle in the current buffer */
                    System.arraycopy(cb, markedChar, cb, 0, delta);
                    markedChar = 0;
                    dst = delta;
                } else {
                    /* Reallocate buffer to accommodate read-ahead limit */
                    char ncb[] = new char[readAheadLimit];
                    System.arraycopy(cb, markedChar, ncb, 0, delta);
                    cb = ncb;
                    markedChar = 0;
                    dst = delta;
                }
                nextChar = nChars = delta;
            }
        }

        int n;
        do {
            n = in.read(cb, dst, cb.length - dst);
        } while (n == 0);
        if (n > 0) {
            nChars = dst + n;
            nextChar = dst;
        }

    }

    String readToken(char delim) throws IOException {
        StringBuffer s = null;
        int startChar;
        boolean omitLF = skipLF;
        bufferLoop:
        for (; ; ) {
            if (nextChar >= nChars) {
                fill();
            }
            if (nextChar >= nChars) {
                if (s != null && s.length() > 0) {
                    return s.toString();
                } else {
                    return null;
                }
            }
            boolean eol = false;
            char c = 0;
            int i;
            if (omitLF && (cb[nextChar] == delim || cb[nextChar] == '\n' || cb[nextChar] == '\r')) {
                nextChar++;
            }
            skipLF = false;
            omitLF = false;
            charLoop:
            for (i = nextChar; i < nChars; i++) {
                c = cb[i];
                if (c == delim || c == '\n' || c == '\r') {
                    eol = true;
                    break charLoop;
                }
            }
            startChar = nextChar;
            nextChar = i;
            if (eol) {
                String str;
                if (s == null) {
                    str = new String(cb, startChar, i = startChar);
                } else {
                    s.append(cb, startChar, i - startChar);
                    str = s.toString();
                }
                nextChar++;
                if (c == delim || c == '\n' || c == '\r') {
                    skipLF = true;
                }
                return str;
            }
            if (s == null) {
                s = new StringBuffer(defaultExpectedLineLength);
            }
            s.append(cb, startChar, i - startChar);
        }

    }

    public static void main(String[] args) throws Exception {
        FileReader in = new FileReader("/Users/zhaobing/myinfo/code/designmode/temp/log.txt");
        MyBufferedReader in2 = new MyBufferedReader(in, 1024);
        String s = "";
        while ((s = in2.readToken('-')) != null) {
//            System.out.println(n);
            System.out.println(s);
        }
        in2.close();
        in.close();
    }


}
